﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
namespace Test {
	class Zop {
		static MAL dal=new MAL();
		static ZCache pld=new ZCache("zop");
		static void MainX() {

			//mal.Scalar("drop table prop;create table prop(id int auto_increment primary key,pkey int unique,inserted datetime,title varchar(50),mrk varchar(50),stat varchar(20),adr varchar(500),price int,lat double,lgt double,pmod varchar(20),pcode varchar(5))");
			foreach(string lnkf in pld.s.GetValue("mai") as string[])
				for(int i=1;i<=6;i++)
				{
					string lnk=string.Format(lnkf,i,30);
					Console.Write(lnk);
					string cont=pld.Get(lnk,60);
					//html2struct.htmlStruct stru=new html2struct.htmlStruct(cont);
					//return;
					Console.WriteLine(" - "+cont.Length);
					string[] lis=Nod.Dbls(cont,"<li data-listing-id=");
					foreach(string str in lis)
					{
						string[] ancs=Nod.Dbls(str,"<a");
						//if(ancs.Length!=10)
						//continue;
						{
							string pkey=Nod.Rx(ancs[0],"data-ajax=\"(\\d+?)\"");

							string added=Nod.InnerText(Nod.Find(str,"Added on")).Replace("Added on","").Trim();
							added=Regex.Replace(added,"st|nd|rd|th","");
							DateTime datAdded=DateTime.ParseExact(added,"d MMM yyyy",null);

							string stat=Nod.Dbl(str,"<span class=\"status-text");
							if(stat!=null)
								stat=Nod.InnerText(stat);
							else
								stat="";



							int cnt=dal.NonQry("update prop set lastseen=now(),stat='{1}' where pkey={0}",pkey,stat);
							//long cnt=(long)mal.Scalar("select count(*)from prop where pkey={0}",pkey);
							if(cnt>0)
							{
								continue;
							}
							//string link=string.Format("http://www.zoopla.co.uk/for-sale/details/{0}",pkey);

							string titleheader=Nod.Dbl(str,"<h2");
							string title=Nod.InnerText(Nod.Dbl(titleheader,"<a"));
							if(!ancs[3].Contains("&pound;"))
								continue;
							string poa=Nod.InnerText(ancs[3]);

							string pri=Nod.Rx(ancs[3],">[^<]+?&pound;(.+?)<").Trim().Replace(",","");

							Console.WriteLine(pkey+" - "+pri);
							string pm=Nod.InnerText(Nod.Dbl(ancs[3],"<span class='price-modifier'>"));
							string adr=Nod.InnerText(ancs[5]);
							string[] adrArr=adr.Split(' ');
							string pcode=adrArr[adrArr.Length-1];
							string lat=Nod.Rx(str,"<meta itemprop=\"latitude\" content=\"(.+?)\" />");
							string lgt=Nod.Rx(str,"<meta itemprop=\"longitude\" content=\"(.+?)\" />");
							string mrk="";
							string mrkTmp=Nod.Dbl(str,"<div class=\"agent_logo\">");
							if(mrkTmp!=null)
								mrk=System.Web.HttpUtility.HtmlDecode(Nod.Rx(mrkTmp,"alt=\"Marketed by (.+?)\"")).Trim();
							dal.NonQry("insert into prop(inserted,lastseen,adr,price,lat,lgt,pkey,pmod,title,stat,pcode,mrk,listed)values(now(),now(),'{0}',{1},{2},{3},{4},'{5}','{6}','{7}','{8}','{9}','{10:yyyy-MM-dd}')",adr.Replace("'","''"),pri,lat,lgt,pkey,pm,title,stat,pcode,mrk.Replace("'","''"),datAdded.ToString("yyyy-MM-dd"));
						}
					}
				}
		}
		static void MainC() {
			DataTable tab=dal.Query("show tables");
		}
	}
}
